﻿using System;
using System.Collections.Generic;
using System.Text;

namespace dotServer
{
	/// <summary>
	/// パスを表す
	/// </summary>
	internal class Path
	{
		#region 

		//	パスの名前
		private string _Name;
		//	親
		private Path _Parent;
		//	子供
		private Path _Child;

		#endregion


		#region プロパティ

		public int Depth
		{
			get
			{
				if( _Child==null ) return 1;
				int i = 0;
				Path p = this;
				while(p!=null)
				{
					++i;
					p = p._Child;
				}
				return i;
			}
		}

		#endregion


		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="path"></param>
		public Path ( string path )
		{
			_Name = "ROOT";
			if( string.IsNullOrEmpty(path) )
			{
				_Child = null;
				return;
			}

			char[] sepa = { '/' };
			string[] __paths = path.Split( sepa );

			Path __wp = this;
			int I = __paths.Length;
			for( int i=0 ; i<I ; ++i )
			{
				string __s = __paths[i];
				if( ".".Equals( __s ) || __s.Length==0 ) continue;
				if( "..".Equals( __s) )
				{
					if( __wp._Parent !=null ) __wp = __wp._Parent;
					continue;
				}
				__wp = new Path( __wp, __s );
			}
		}


		/// <summary>
		/// コンストラクタ
		/// </summary>
		/// <param name="parent">親</param>
		/// <param name="name">名前</param>
		private Path ( Path parent, string name )
		{
			_Name = name;
			_Child = null;
			_Parent = parent;
			parent._Child = this;
		}
	}
}
